package com.yanftch.jsonformat.json2ets.generator

import com.intellij.notification.*
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.ProjectManager

class MessageDelegate {

    fun catchException(throwable: Throwable, event: AnActionEvent?) {
        val message = if (throwable.message != null) {
            "json2ts error: ${throwable.message}"
        } else {
            "json2ts error"
        }
        showErrorMessage(message, event)
    }


    fun showErrorMessage(message: String?, event: AnActionEvent?) {
        sendNotification(message, event, NotificationType.ERROR)
    }

    fun showMessage(message: String?, event: AnActionEvent?) {
        sendNotification(message, event, NotificationType.INFORMATION)
    }

    private fun sendNotification(message: String? = "", event: AnActionEvent?, type: NotificationType = NotificationType.INFORMATION) {
        if (event == null) return
        NotificationGroupManager.getInstance().getNotificationGroup("id_jsonformat_toast").createNotification("", message ?: "", type)
            .notify(event.project)
    }
}
